<template>
  <el-dialog title="单位" :visible.sync="dialogFormVisible" width="500px" append-to-body @close="close" :close-on-click-modal="false" v-dialogDrag>
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.unitCode" placeholder="请输入编码" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item>
            <el-input v-model.trim="queryForm.unitName" placeholder="请输入名称" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table border v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column label="选择" width="60" align="center">
        <template slot-scope="scope">
          <el-radio :label="scope.$index" v-model="radio" @change.native="getCurrentRow(scope.row)">{{ '' }}</el-radio>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="unitCode" label="编码"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" min-width="100" prop="unitName" label="名称"></el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getList } from '@/api/basics/unit'

  export default {
    name: 'ProductUnit',
    data() {
      return {
        dialogFormVisible: false,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          unitName: undefined,
        },
        currentRow: undefined,
        radio: undefined,
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      getCurrentRow(row) {
        this.currentRow = row
      },
      save() {
        this.$emit('fetch-data', this.currentRow)
        this.close()
      },
      showEdit() {
        this.dialogFormVisible = true
      },
      close() {
        this.dialogFormVisible = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.list
        this.total = data.total
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
